//
//  MessageViewController.m
//  GameBaby
//
//  Created by iOS on 14-2-26.
//  Copyright (c) 2014年 keyZhang. All rights reserved.
//

#import "MessageViewController.h"
#import "DataService.h"
#import "MsgModel.h"
@interface MessageViewController ()

@end

@implementation MessageViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        UILabel *t = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 90, 30)];
        t.font = [UIFont systemFontOfSize:16];
        t.textColor = [UIColor blackColor];
        t.backgroundColor = [UIColor clearColor];
        t.textAlignment = UITextAlignmentCenter;
        t.text = @"消息中心";
        self.navigationItem.titleView = t;

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"an_03.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(rightBarButton:) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 55, 30);
    //创建导航按钮
//    UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
//    self.navigationItem.rightBarButtonItem = backBarButtonItem;
    self.view.backgroundColor = [UIColor whiteColor];
    
    __weak MessageViewController *this = self;
    self.messageTV.finishBlock = ^{
        [this _loadMsgMoreData];
    };

    _msgdataList = [NSMutableArray array];
    
    self.messageTV.refreshDelegate = self;


    [self loadMsgData];
}

- (void)loadMsgData
{
    NSString *urlStr = [NSString stringWithFormat:@"http://m.a.ewoka.com/apply/get_usermsg"];
    NSMutableDictionary *dicData = [NSMutableDictionary dictionaryWithObjectsAndKeys:[[NSUserDefaults standardUserDefaults]objectForKey:@"appid"],@"appid", nil];
    __weak MessageViewController *this = self;

    [DataService requestWithURL:urlStr
                         params:dicData
                     httpMethod:@"POST"
                    finishBlock:^(id result) {
                        [this loadZXDataFinished:result];
                    }];
}


- (void)_loadMsgMoreData
{
    _msgdataList = [NSMutableArray array];
    [self loadMsgData];
}

- (void)loadZXDataFinished:(NSDictionary *)result
{
    NSArray *array = [result objectForKey:@"list"];

    if (array.count < 10) {
        self.messageTV.isMore = NO;
        for (NSDictionary *dic in array) {
            MsgModel *model = [[MsgModel alloc] initContentWith:dic];
            [self.msgdataList addObject:model];
        }
        self.messageTV.dataList = self.msgdataList;
        [self.messageTV reloadData];
        [self.messageTV doneLoadingTableViewData];
    }else
    {
        for (NSDictionary *dic in array) {
            MsgModel *model = [[MsgModel alloc] initContentWith:dic];
            [self.msgdataList addObject:model];
        }
        self.messageTV.dataList = self.msgdataList;
        [self.messageTV reloadData];
        [self.messageTV doneLoadingTableViewData];
        if (self.msgdataList.count > 99) {
            self.messageTV.isMore = NO;
        }else
        {
            self.messageTV.isMore = NO;
        }
    }
}

#pragma mark - BaseTableView delegate
//上拉加载的协议方法
- (void)pullUp:(BaseTableView *)tableView
{
    NSString *str = [(UIButton *)[tableView valueForKey:@"moreButton"] titleLabel].text;
    if (![str isEqualToString:@"加载更多数据"]) {
        return;
    }else
    {
        [self loadMsgData];
    }
}

- (void)rightBarButton:(UIButton *)btn
{
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)dealloc
{
    
}

@end
